package com.jsu.controller;

import com.jsu.sender.MessageSender;
import com.jsu.service.AgendaService;
import com.jsu.service.StreamingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.FrameRecorder;
import org.checkerframework.checker.units.qual.A;
import org.example.model.activity.dto.AgendaDto;
import org.example.model.activity.dto.AgendaWithFlowDTO;
import org.example.model.activity.pojo.Agenda;
import org.example.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.task.TaskExecutor;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.concurrent.CompletableFuture;

@RestController
@Slf4j
@RequestMapping("/api/agenda")
@Api(tags = "议程控制器")
@AllArgsConstructor
public class AgendaController {
    @Autowired
    private AgendaService agendaService;
    @Autowired
    private StreamingService streamingService;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询议会详情")
    public ResponseResult getAgendaById(
            @ApiParam(value = "议程会id",required = true)
            @PathVariable("id") int id){
        return agendaService.getAgendaById(id);
    }
    @ApiOperation(value = "议程浏览")
    @PutMapping("/addAgendaViews")
    public ResponseResult addAgendaViews(Integer agendaId){
        return agendaService.addAgendaViews(agendaId);
    }



    @GetMapping("/getAllAgendas")
    @ApiOperation(value = "展示所有议程信息")
    public ResponseResult getAllAgendas(Date date,Integer type,String name,Integer userId){
        ResponseResult responseResult = agendaService.getAllAgendas(date,type,name,userId);
        System.out.println(responseResult.toString());
        return responseResult;
    }

    @PostMapping("/addAgenda")
    @ApiOperation(value = "新增一个议程信息")
    public ResponseResult addOneAgenda(@RequestBody AgendaWithFlowDTO agendaWithFlowDTO){
        return agendaService.addOneAgenda(agendaWithFlowDTO);
    }

    @PutMapping("/updateAgenda")
    @ApiOperation(value = "更新议程信息")
    public ResponseResult updateAgenda(@RequestBody AgendaWithFlowDTO agendaWithFlowDTO){
        return agendaService.updateAgenda(agendaWithFlowDTO);
    }

    @DeleteMapping("/delAgenda")
    @ApiOperation(value = "删除议程信息")
    public ResponseResult delAgenda(Integer agendaId){
        return agendaService.delAgenda(agendaId);
    }
    @GetMapping("/getVideoLink/{id}")
    @ApiOperation(value = "获取直播回放链接")
    public ResponseResult getVideoLink(@ApiParam(value = "议程会id",required = true)
                                       @PathVariable("id") int id){
        return agendaService.getVideoLink(id);
    }

    @GetMapping("/getLiveLink/{id}")
    @ApiOperation(value = "获取直播链接")
    public ResponseResult getLiveLink(@ApiParam(value = "议程会id",required = true)
                                      @PathVariable("id") int id){
        return agendaService.getLiveLink(id);
    }

    @PostMapping("/updateSubscriber")
    @ApiOperation(value = "更新订阅人数")
    public ResponseResult updateSubscriber(@ApiParam(value = "议程会id",required = true)
                                            int agendaId,
                                        @ApiParam(value = "用户id",required = true)
                                        int userId,
                                        @ApiParam(value = "取消标识",required = true)
                                        int isCancled){
        return agendaService.updateSubscriber(agendaId,userId,isCancled);
    }


    @PostMapping("/start-live-stream")
    @ApiOperation(value = "开启会议直播")
    public ResponseResult startLiveStream(@ApiParam(value = "会议id")@RequestParam("agendaId") int id,
                                          @ApiParam(value = "会议名")@RequestParam("agendaName")String name){

        try {
            streamingService.recordWebcamAndMicrophone(id);
            //MessageSender.sendAgendaLive(name, String.valueOf(id));
        } catch (FrameGrabber.Exception e) {
            throw new RuntimeException("开启失败！");
        } catch (FrameRecorder.Exception e) {
            throw new RuntimeException("开启失败");
        }
        return ResponseResult.okResult("开启成功！");
    }

    @PostMapping("/stop-live-stream")
    @ApiOperation(value = "关闭会议直播")
    public ResponseResult stopLiveStream(){

        try {
            streamingService.stopLive();
        }catch (Exception e){
            throw new RuntimeException("关闭直播失败！");
        }
        return ResponseResult.okResult("关闭直播成功！");
    }


}
